package iO;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @Author: Miyako
 * @Date: 2024-07-16-14:30
 * @Description:字符串转根据UTF-8编码规则存入bit数组然后写入字符串,最后通过String构造方法输出
 */
public class WriteStringDemo01 {
    public static void main(String[] args) throws IOException {
        String str = "我爱java ";
        FileOutputStream f1= new FileOutputStream("./1.txt");
        byte[] b = str.getBytes(StandardCharsets.UTF_8);
        f1.write(b);
        f1.write(" yuanshenqidong".getBytes(StandardCharsets.UTF_8));
        FileInputStream f2 = new FileInputStream("./1.txt");
        File file = new File("./1.txt");
//        byte[] bit = new byte[(int)file.length()];
//        int len = f2.read(bit);
        byte[] bit= new byte[f2.available()];//创建一个长度与流中有效字节数匹配的数组
        f2.read(bit);//一次性读取所有数据
        String str1 = new String(bit);
        System.out.println(str1);



    }
}
